package com.example.administrator.siku;

import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.siku.fragments.GoodsFragment;
import com.example.administrator.siku.fragments.MenuFragment;
import com.example.administrator.siku.fragments.MineFragment;
import com.example.administrator.siku.fragments.ShoppingFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private RadioButton home;
    private RadioButton goods;
    private RadioButton shopping;
    private RadioButton mine;
    private List<Fragment> fragments;
    private int currentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.nemu:
                        setFragment(0);
                        break;
                    case R.id.goods:
                        setFragment(1);
                        break;
                    case R.id.shopping:
                        setFragment(2);
                        break;
                    case R.id.mine:
                        setFragment(3);
                        break;
                }
            }
        });
    }

    private void initData() {
        fragments = new ArrayList<>();
        MenuFragment fragment0 = new MenuFragment();
        GoodsFragment fragment1 = new GoodsFragment();
        ShoppingFragment fragment2 = new ShoppingFragment();
        MineFragment fragment3 = new MineFragment();
        fragments.add(fragment0);
        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);
        getSupportFragmentManager().beginTransaction().add(R.id.container,fragments.get(0)).commit();
    }

    private void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.main_rg);
        home = (RadioButton) findViewById(R.id.nemu);
        goods = (RadioButton) findViewById(R.id.goods);
        shopping = (RadioButton) findViewById(R.id.shopping);
        mine = (RadioButton) findViewById(R.id.mine);


        Drawable drawable_home = getResources().getDrawable(R.drawable.menu_bg);
        drawable_home.setBounds(0, 0, 60, 60);
        home.setCompoundDrawables(null, drawable_home, null, null);

        Drawable drawable_goods = getResources().getDrawable(R.drawable.goods_bg);
        drawable_goods.setBounds(0, 0,  60, 60);
        goods.setCompoundDrawables(null, drawable_goods, null, null);

        Drawable drawable_shopping = getResources().getDrawable(R.drawable.shopping_bg);
        drawable_shopping.setBounds(0, 0, 60, 60);
        shopping.setCompoundDrawables(null, drawable_shopping, null, null);

        Drawable drawable_mine = getResources().getDrawable(R.drawable.mine_bg);
        drawable_mine.setBounds(0, 0, 60, 60);
        mine.setCompoundDrawables(null, drawable_mine, null, null);
    }
        private void setFragment(int index){
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            Fragment currentFragment = fragments.get(currentIndex);
            Fragment targetFragment = fragments.get(index);
            if (!targetFragment.isAdded()) {
                transaction.add(R.id.container, targetFragment).hide(currentFragment).commit();
            } else {
                transaction.show(targetFragment).hide(currentFragment).commit();
            }
            currentIndex = index;
        }
}
